﻿using Interface.Cache;
using Interface.Core.Controllers;
using Interface.Core.Utility;
using Interface.Mvc.Areas.Scrm.Models;
using Interface.Utility.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Interface.Mvc.Areas.Scrm.Controllers
{
    public class ExchangeController : CoreController
    {
        private IEnumerable<MemberSource> Sources
        {
            get
            {
                return CommonCache.GetCache("Scrm_Member_Sources", () =>
                {
                    return db.Scrm.GetList<MemberSource>();
                });
            }
        }

        /// <summary>
        /// 提交兑换订单
        /// url: /scrm/exchange/exchange
        /// params: memberid, idlist, countlist, username, mobile, useraddress, remark
        /// </summary>
        [HttpPost]
        public string Exchange(int memberid, string idlist, string countlist, string username, string mobile, string useraddress, string remark)
        {
            ResultEntity re = db.Crm.StoredQuery<ResultEntity>("Scrm_Sp_Exchange", new
            {
                memberid = memberid,
                idlist = idlist,
                countlist = countlist,
                username = username,
                mobile = mobile,
                useraddress = useraddress,
                remark = remark
            }).FirstOrDefault();

            if (re.success)
            {
                string sql = "select CharRelate from Member_Relate where SourceID = @sourceid and MemberID = @memberid";
                int sourceid = Sources.FirstOrDefault(m => m.Code == "taste").ID;
                string openid = db.Scrm.ExecuteScalar<string>(sql, new { sourceid, memberid });
                if (!string.IsNullOrEmpty(openid))
                {
                    try
                    {
                        Dictionary<string, object> dic = new Dictionary<string, object>();
                        dic.Add("source", Sources.FirstOrDefault(m => m.Code == appid).Name);
                        dic.Add("scrmid", memberid);
                        dic.Add("openid", openid);
                        dic.Add("points", "-" + re.data);
                        dic.Add("remark", "兑换产品扣减微笑值");
                        HttpHelper.Post("https://www.uvmami.com/interface/pointschangetemplate?key=engnice", dic);
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            return ResultUtility.UnknownResult(re.success, re.imessage, re.imessage);
        }

        /// <summary>
        /// 新版提交兑换订单
        /// url: /scrm/exchange/exchange
        /// params: memberid, idlist, limitidlist, countlist, username, mobile, useraddress, remark
        /// </summary>
        [HttpPost]
        public string ExchangeNew(int memberid, string idlist, string limitidlist, string countlist, string username, string mobile, string useraddress, string remark)
        {
            ResultEntity re = db.Crm.StoredQuery<ResultEntity>("Scrm_Sp_ExchangeNew", new
            {
                memberid = memberid,
                idlist = idlist,
                limitidlist = limitidlist,
                countlist = countlist,
                username = username,
                mobile = mobile,
                useraddress = useraddress,
                remark = remark
            }).FirstOrDefault();

            if (re.success)
            {
                string sql = "select CharRelate from Member_Relate where SourceID = @sourceid and MemberID = @memberid";
                int sourceid = Sources.FirstOrDefault(m => m.Code == "taste").ID;
                string openid = db.Scrm.ExecuteScalar<string>(sql, new { sourceid, memberid });
                if (!string.IsNullOrEmpty(openid))
                {
                    try
                    {
                        Dictionary<string, object> dic = new Dictionary<string, object>();
                        dic.Add("source", Sources.FirstOrDefault(m => m.Code == appid).Name);
                        dic.Add("scrmid", memberid);
                        dic.Add("openid", openid);
                        dic.Add("points", "-" + re.data);
                        dic.Add("remark", "兑换产品扣减微笑值");
                        HttpHelper.Post("https://www.uvmami.com/interface/pointschangetemplate?key=engnice", dic);
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            return ResultUtility.UnknownResult(re.success, re.imessage, re.imessage, re.data);
        }

        /// <summary>
        /// 提交限兑订单
        /// url: /scrm/exchange/limitexchange
        /// params: memberid, productid, productcount, limitid, username, mobile, useraddress, remark
        /// </summary>
        [HttpPost]
        public string LimitExchange(int memberid, int productid, int productcount, int limitid, string username, string mobile, string useraddress, string remark)
        {
            ResultEntity re = db.Crm.StoredQuery<ResultEntity>("Scrm_Sp_LimitExchange", new
            {
                memberid = memberid,
                productid = productid,
                productcount = productcount,
                limitid = limitid,
                username = username,
                mobile = mobile,
                useraddress = useraddress,
                remark = remark
            }).FirstOrDefault();

            if (re.success)
            {
                string sql = "select CharRelate from Member_Relate where SourceID = @sourceid and MemberID = @memberid";
                int sourceid = Sources.FirstOrDefault(m => m.Code == "taste").ID;
                string openid = db.Scrm.ExecuteScalar<string>(sql, new { sourceid, memberid });
                if (!string.IsNullOrEmpty(openid))
                {
                    try
                    {
                        Dictionary<string, object> dic = new Dictionary<string, object>();
                        dic.Add("source", Sources.FirstOrDefault(m => m.Code == appid).Name);
                        dic.Add("scrmid", memberid);
                        dic.Add("openid", openid);
                        dic.Add("points", "-" + re.data);
                        dic.Add("remark", "兑换产品扣减微笑值");
                        HttpHelper.Post("https://www.uvmami.com/interface/pointschangetemplate?key=engnice", dic);
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            return ResultUtility.UnknownResult(re.success, re.imessage, re.imessage);
        }

        /// <summary>
        /// 兑换订单发货
        /// url: /scrm/exchange/exchangedelivery
        /// params: ordercode
        /// </summary>
        [HttpPost]
        public string ExchangeDelivery(string ordercode)
        {
            dynamic result = db.Crm.StoredQuery<dynamic>("Scrm_Sp_ExchangeDelivery", new { OrderCode = ordercode }).FirstOrDefault();

            if (result.success)
            {
                try
                {
                    Dictionary<string, object> dic = new Dictionary<string, object>();
                    dic.Add("openid", result.OpenId);
                    dic.Add("deliveryName", result.DeliveryName);
                    dic.Add("deliveryCode", result.DeliveryCode);
                    dic.Add("product", result.Product);
                    dic.Add("count", result.iCount);
                    HttpHelper.Post("https://www.uvmami.com/interface/exchangeorderdeliverytemplate?key=engnice", dic);
                }
                catch (Exception)
                {
                }
            }

            return ResultUtility.UnknownResult(result.success, result.imessage, result.imessage);
        }

        /// <summary>
        /// 获取兑换次数
        /// url: /scrm/exchange/getcount
        /// params: memberid
        /// </summary>
        [HttpGet]
        public string GetCount(int memberid)
        {
            ResultEntity re = db.Crm.StoredQuery<ResultEntity>("Scrm_Sp_ExchangeCount", new
            {
                MemberId = memberid
            }).FirstOrDefault();

            return ResultUtility.SuccessResult(data: re.data);
        }

        /// <summary>
        /// 兑换明细列表
        /// url: /scrm/exchange/getlist
        /// params: memberid, itype
        /// </summary>
        [HttpGet]
        public string GetList(int memberid, string itype)
        {
            List<dynamic> list = db.Crm.StoredQuery<dynamic>("Scrm_Sp_ExchangeInfoPage", new
            {
                MemberId = memberid,
                iType = itype
            }).ToList();

            return ResultUtility.SuccessResult(data: list);
        }
    }
}
